///////////////////////////////////////////////////
// Election Administration Polarization
// Estimate the Clerk Incumbency Advantage
///////////////////////////////////////////////////

gl path "~/Library/CloudStorage/GoogleDrive-danmckinleythompson@gmail.com/My Drive/ElecAdminPolarization/How_Partisan_Is_Local_Election_Admin_Replication"

* Prepare data on the results of the next election, even if it is uncontested, using the diff-in-diff data
use "$path/analysis_data/elec_did_data.dta", clear
drop if unexpected_timing
keep state county next_elec dem
duplicates drop
duplicates tag state county next_elec, gen(tag)
drop if tag
rename (next_elec dem) (year pre_dem)
tempfile pre
save `pre'

* Put together the RD and previous election data
use "$path/analysis_data/elec_rd_data.dta", clear
keep if policy_year==year+1
gen post_dem = vs_dem_leo>0.5 if vs_dem_leo!=.
merge 1:1 state county year using `pre', keep(1 2 3)

* Combined density plot, i.e., are Ds more likely to win close elections in
* counties controlled by Ds and more likely to lose in close elections
* in counties controlled by Rs?
gen vs_in_charge_leo = 1 - vs_dem_leo if pre_dem==0
replace vs_in_charge_leo = vs_dem_leo if pre_dem==1
rddensity vs_dem_leo, c(0.5) ///
	plot nohist esll_opt(lc(gs2)) eslr_opt(lc(gs2)) ///
		cirl_opt(fc(gs10) lc(gs10)) cirr_opt(fc(gs10) lc(gs10)) ///
		graph_opt(graphregion(color(white)) legend(off) scale(1.4) ///
		yti("Density") xti("In-Power Party Elec Official Vote Share"))
graph export "$path/output/rd_density.pdf", replace

